package com.example.doctorstation.POJOController;

import com.example.doctorstation.Config.MyResponse;
import com.example.doctorstation.Helper.StringHelper;
import java.util.ArrayList;
import com.example.doctorstation.POJOData.MetaData;
import com.example.doctorstation.POJOMapper.MetaDataMapper;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MetaDataController {


    @Autowired
    MetaDataMapper metaDataMapper;

    @GetMapping(value="/metadata")
    public MyResponse getRecord(MetaData info) {
        try
        {
            if(!StringHelper.isNullOrEmpty(info.getMetaDataName()))
            {
                MetaData m= metaDataMapper.getAllMetaDataValueByName(info.getMetaDataName());
                return new MyResponse(packList(m), "已成功获取该元数据！", "不存在该元数据");
            }
            if(info.getMetaDataID() >0)
            {
                MetaData m= metaDataMapper.getAllMetaDataValueByID(info.getMetaDataID());
                return new MyResponse(packList(m), "已成功获取该元数据！", "不存在该元数据");
            }

            return new MyResponse(metaDataMapper.getAllMetaDataNames(), "获取所有元数据定义！", "没有获取所有元数据定义");
        }
        catch (Throwable e)
        {
            e.printStackTrace();
            return new MyResponse();
        }
    }

    // 前端要求格式{”data“：[{"label":"男","value":1},{"label":"女","value":2}]}
    private ArrayList<DomainLabel> packList(MetaData m)
    {
        String[] valueArray = m.getMetaDataValue().split("\\|");
        ArrayList<DomainLabel> list = new ArrayList<>();
        int i = 0;
        for (String value:valueArray)
        {
            i++;
            list.add(new DomainLabel(value,i));
        }
        return list;
    }
}


@Data
class DomainLabel
{
    private String label;
    private Integer value;
    public DomainLabel()
    {

    }
    public DomainLabel(String label,Integer value)
    {
        this.label = label;
        this.value = value;
    }
}